package twel.probenprophet.helden.heroes.helden5;

import java.io.Writer;
import java.util.Collection;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import twel.probenprophet.base.exceptions.ProbenprophetException;
import twel.probenprophet.helden.heroes.helden5.jaxb.HeldenRootElement;
import twel.probenprophet.helden.heroes.helden5.jaxb.Hero;

public class HeroXmlWriter {

	public void write(Collection<Hero> heroes, Writer writer) {
		HeldenRootElement rootElement = new HeldenRootElement();
		rootElement.setHeroes(heroes);

		try {
			JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
			Marshaller marshaller = context.createMarshaller();

			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "helden.xsd");

			marshaller.marshal(rootElement, writer);
		} catch (JAXBException e) {
			throw new ProbenprophetException("JAXB marshalling failed", e);
		}
	}

}
